var  BASE = BASE||{}; //全局公用方法
$(function () {
    BASE.COMMON.urlLeftNav();
    BASE.COMMON.recommendDarenList(1,10);
    $(document).scroll(function () {
        BASE.COMMON.scrollEvent(BASE.PROJECT.getData);
    });
});

BASE.PROJECT = (function () {
    var PAGE = 2;
    /*弹出层*/
    function showPopup(obj) {
        BASE.PROJECTCOMMON.getQQList();
        $('#title').val('');
        $('#price').val('');
        BASE.UPLOADANSWER.ue.txt.html('');
        $('#order_price').val('');
        var userId = BASE.COMMON.getUserId();
        if(!userId){
            location.href = '/login.html';
            return;
        }
        var flag = $(obj).data('flag');
        if(flag){
            $('#answer_pop').children('.pop-answer').removeClass('is-full-screen');
            $('#answer_pop').show();
            return;
        }
        $('#answer_pop').show();
        $(obj).data('flag',true);
    }
    /*发单*/
    function sendOrder(obj) {
        var sendData = _getAddData();
        if(!sendData){
            return;
        }
        $(obj).attr('disabled',true);
        BASE.postHttp('/device/order/project/buy',sendData,function (res) {
            if(res.code != 200){
                BASE.clearpop(res.message || '新增失败');
                $(obj).attr('disabled',false);
                return;
            }
            $('#answer_pop').hide();
            location.href = '/userSubmitOrder.html?id='+res.data.id;
        })
    }
   
    /*获取新增的数据*/
    function _getAddData() {
        var sendData = {
            title: $('#title').val(),
            content: BASE.UPLOADANSWER.ue.txt.html(),
            fileUrl:$('body').data('file-url'),
           /*categoryId:356408066650148864,*/
            price:$('#price').val(),
            priceType:$('input[name="price-type"]:checked').val()
        };
        if(!sendData.title){
            BASE.clearpop('请输入项目标题');
            return false;
        }
        if(!sendData.content){
            BASE.clearpop('请输入项目简介');
            return false;
        }
        if(!sendData.price){
            BASE.clearpop('请输入价格');
            return false;
        }
       /* if(sendData.price<100){
            BASE.clearpop('项目金额最低100元');
            return false;
        }*/
        return sendData;
    }
    /*获取推荐的数据*/
    function getData() {
        var $box = $('#middleListContent');
        $box.find('.loading').show();
        var sendData = {
            pageNum:PAGE,
            pageSize:15,
        };
        BASE.apiGet('/device/project/list',sendData,function (res) {
            ++PAGE;
            $box.find('.loading').hide();
            BASE.COMMON.SCROLLFLAG = true;
            if((res.nextPage == 0 && res.pages > 1) || res.pages <= 1){
                BASE.COMMON.SCROLLFLAG = false;
                if(res.pages!=0 && res.total>=8){
                    $box.find('.nomore-data').show();
                }
            }
            $box.find('.content').append(template('middleListContent_tpl',{data:res.list}));
        })
    }
    return {
        showPopup:showPopup,
        sendOrder:sendOrder,
        getData:getData,
    }
})();